﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using My.GenCode.Entities;
using My.GenCode.Interface;

namespace My.GenCode.Templates
{
    public class GenServiceClass : CodeTemplate
    {
        #region 覆盖父类的方法

        public GenServiceClass(string tpl, IList<EntityInfo> infos,string desc) : base(tpl, infos,desc) { }

        protected override string ProcTemplate()
        {
            _tpl = this.LoopEntities(_tpl, "<DeclareDao>", this.GenDeclareDao);
            _tpl = this.LoopEntities(_tpl, "<DeclareSafe>", this.GenDeclareSafe);

            _tpl = this.LoopEntities(_tpl, "<InitDao>", this.GenInitDao);
            _tpl = this.LoopEntities(_tpl, "<InitSafe>", this.GenInitSafe);

            _tpl = this.LoopEntities(_tpl, "<CodeBlock>", this.GenCodeBlock);

            _tpl = _tpl.Replace("<FileName>", _nh.ServiceClassFileName);                   // 处理<FileName>标签
            _tpl = _tpl.Replace("<NameSpace>", _nh.NameSpace);                       // 处理<NameSpace>标签
            _tpl = _tpl.Replace("<ClassName>", _nh.ServiceClassName);                // 处理<ClassName>标签
            _tpl = _tpl.Replace("<InterfaceNames>", this.InterfaceNames());          // 处理<InterfaceNames>标签

            return _tpl;
        }
        
        protected override string OutputFilename()
        {
            return @"Business\Services\" + _nh.ServiceClassFileName;
        }

        #endregion

        #region 私有方法,处理模板的循环标签
        /// <summary>
        /// 声明Dao类
        /// </summary>
        /// <param name="tpl"></param>
        /// <param name="info"></param>
        /// <returns></returns>
        private string GenDeclareDao(string tpl, EntityInfo info)
        {
            NameHelper nh = new NameHelper(info.Name, info.Service, info.Company);
            tpl = tpl.Replace("<Interface>", nh.InterfaceName);
            tpl = tpl.Replace("<DaoClassName>", nh.DaoClassName);
            return tpl;
        }
        /// <summary>
        /// 初始化Dao类
        /// </summary>
        /// <param name="tpl"></param>
        /// <param name="info"></param>
        /// <returns></returns>
        private string GenInitDao(string tpl, EntityInfo info)
        {
            NameHelper nh = new NameHelper(info.Name, info.Service, info.Company);
            tpl = tpl.Replace("<DaoClassName>", nh.DaoClassName);
            return tpl;
        }

        /// <summary>
        /// 声明Safe类
        /// </summary>
        /// <param name="tpl"></param>
        /// <param name="info"></param>
        /// <returns></returns>
        private string GenDeclareSafe(string tpl, EntityInfo info)
        {
            NameHelper nh = new NameHelper(info.Name, info.Service, info.Company);
            tpl = tpl.Replace("<Interface>", nh.InterfaceName);
            tpl = tpl.Replace("<SafeClassName>", nh.SafeClassName);
            return tpl;
        }

        /// <summary>
        /// 初始化Safe
        /// </summary>
        /// <param name="tpl"></param>
        /// <param name="info"></param>
        /// <returns></returns>
        private string GenInitSafe(string tpl, EntityInfo info)
        {
            NameHelper nh = new NameHelper(info.Name, info.Service, info.Company);
            tpl = tpl.Replace("<DaoClassName>", nh.DaoClassName);
            tpl = tpl.Replace("<SafeClassName>", nh.SafeClassName);
            return tpl;
        }

        /// <summary>
        /// 主体代码块
        /// </summary>
        /// <param name="tpl"></param>
        /// <param name="info"></param>
        /// <returns></returns>
        private string GenCodeBlock(string tpl,EntityInfo info)
        {
            NameHelper nh = new NameHelper(info.Name, info.Service, info.Company);

            tpl = tpl.Replace("<SafeClassName>", nh.SafeClassName);               // 处理<EntityName>标签
            tpl = tpl.Replace("<InterfaceName>", nh.InterfaceName);               // 处理<InterfaceName>标签

            tpl = tpl.Replace("<CreateMethodName>", nh.CreateMethodName);         // 处理<CreateMethodName>标签
            tpl = tpl.Replace("<UpdateMethodName>", nh.UpdateMethodName);         // 处理<UpdateMethodName>标签
            tpl = tpl.Replace("<DeleteMethodName>", nh.DeleteMethodName);         // 处理<DeleteMethodName>标签
            tpl = tpl.Replace("<ReadDetailMethodName>", nh.ReadDetailMethodName); // 处理<ReadDetailMethodName>标签
            tpl = tpl.Replace("<ReadListMethodName>", nh.ReadListMethodName);     // 处理<ReadListMethodName>标签
            tpl = tpl.Replace("<ReadCountMethodName>", nh.ReadCountMethodName);   // 处理<ReadCountMethodName>标签

            tpl = tpl.Replace("<CreateMethodSign>", nh.CreateMethodSign);         // 处理<CreateMethodSign>标签
            tpl = tpl.Replace("<UpdateMethodSign>", nh.UpdateMethodSign);         // 处理<UpdateMethodSign>标签
            tpl = tpl.Replace("<DeleteMethodSign>", nh.DeleteMethodSign);         // 处理<DeleteMethodSign>标签
            tpl = tpl.Replace("<ReadDetailMethodSign>", nh.ReadDetailMethodSign); // 处理<ReadDetailMethodSign>标签
            tpl = tpl.Replace("<ReadListMethodSign>", nh.ReadListMethodSign);     // 处理<ReadListMethodSign>标签
            tpl = tpl.Replace("<ReadCountMethodSign>", nh.ReadCountMethodSign);   // 处理<ReadCountMethodSign>标签

            return tpl;
        }

        /// <summary>
        /// 接口名列表
        /// </summary>
        private string InterfaceNames()
        {
            List<string> list = new List<string>();
            NameHelper nh;
            foreach (EntityInfo e in _infos)
            {
                nh = new NameHelper(e.Name, e.Service, e.Company);
                list.Add(nh.InterfaceName);
            }
            return string.Join(",", list.ToArray());
        }

        #endregion
    }
}